package com.uyayo.portal.controller;

import com.uyayo.cms.entity.CmsArticle;
import com.uyayo.cms.entity.CmsChannel;
import com.uyayo.cms.model.CmsArticleCondition;
import com.uyayo.cms.service.CmsArticleService;
import com.uyayo.cms.service.CmsChannelService;
import com.uyayo.cms.service.CmsSiteService;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.portal.biz.CmsSiteBiz;
import com.uyayo.portal.biz.ParamBiz;
import com.uyayo.system.authority.entity.SysUser;
import com.uyayo.ucenter.entity.Member;
import com.uyayo.ucenter.entity.MemberVisitorLog;
import com.uyayo.ucenter.model.MemberVisitorLogCondition;
import com.uyayo.ucenter.service.MemberService;
import com.uyayo.ucenter.service.MemberVisitorLogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * <p>
 * Company: www.lelekeji.com
 * </p>
 * 
 * @author 乐乐科技.胡逢君
 * @date 2016-4-23下午4:03:35
 * @version 1.0
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private final ParamBiz paramBiz;

	private final CmsSiteBiz cmsSiteBiz;

	private final CmsSiteService cmsSiteService;

	private final CmsChannelService cmsChannelService;

	private final CmsArticleService cmsArticleService;

	private  final MessageSource messageSource;


	private final MemberService memberService;

	private final MemberVisitorLogService memberVisitorLogService;
	@Autowired
	public BlogController(MessageSource messageSource, CmsArticleService cmsArticleService, CmsChannelService cmsChannelService, CmsSiteService cmsSiteService, CmsSiteBiz cmsSiteBiz, ParamBiz paramBiz, MemberService memberService, MemberVisitorLogService memberVisitorLogService) {
		this.messageSource = messageSource;
		this.cmsArticleService = cmsArticleService;
		this.cmsChannelService = cmsChannelService;
		this.cmsSiteService = cmsSiteService;
		this.cmsSiteBiz = cmsSiteBiz;
		this.paramBiz = paramBiz;
		this.memberService = memberService;
		this.memberVisitorLogService = memberVisitorLogService;
	}


	/**
	 * 博客频道页跳转-先转发到列表第一页
	 */
	@RequestMapping(value={"index.html","index",""},method=RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView,@RequestParam(value="page", required=false, defaultValue="1") Integer page) {
		modelAndView.addObject("page",page);
		CmsChannel cmsChannel = new CmsChannel();
		cmsChannel.setChannelDir("blog");
		cmsChannel = cmsChannelService.selectOne(cmsChannel);

		CmsArticleCondition cmsArticleCondition = new CmsArticleCondition();
		PageBean<CmsArticle> pageBean = cmsArticleService.selectArticleByPaging(page,3,null,null,cmsArticleCondition);

		modelAndView.addObject("pageList",pageBean);
		modelAndView.addObject("cmsChannel",cmsChannel);
		Subject subject = SecurityUtils.getSubject();
		Member member = (Member) subject.getPrincipal();
		modelAndView.addObject("member",member);

		modelAndView.setViewName(paramBiz.getWebsitTemplatesPrefix()+"blog/list");
		return modelAndView;
	}

	/**
	 * 博客主页跳转
	 */
	@RequestMapping(value={"home/{username}"},method=RequestMethod.GET)
	public ModelAndView home(ModelAndView modelAndView, @PathVariable("username") String username) {
		Member member = memberService.getMemberByUsername(username);
		if(member == null){
			modelAndView.setViewName("/404");
		}
		modelAndView.setViewName(paramBiz.getWebsitTemplatesPrefix()+"index");
		return modelAndView;	
	}

	/**
	 * 内容列表页跳转-重定向到博主列表页
	 */
	@RequestMapping(value={"{username}/article"},method=RequestMethod.GET)
	public ModelAndView article_list(ModelAndView modelAndView, @PathVariable("username") String username) {
		Member member = memberService.getMemberByUsername(username);
		if(member == null){
			modelAndView.setViewName("redirect:/index");
			return modelAndView;
		}
		modelAndView.setViewName("forward:/blog/"+username+"/article/list_1");
		return modelAndView;
	}

	/**
	 * 列表页跳转
	 */
	@RequestMapping(value={"{username}/article/list_{page}"},method=RequestMethod.GET)
	public ModelAndView list(ModelAndView modelAndView, @PathVariable("page") Integer page, @PathVariable("username") String username) {
		modelAndView.addObject("page",page);
		Member member = memberService.getMemberByUsername(username);
		if(member == null){
			modelAndView.setViewName("redirect:/index");
			return modelAndView;
		}
		CmsChannel cmsChannel = new CmsChannel();
		cmsChannel.setChannelDir("blog");
		cmsChannel = cmsChannelService.selectOne(cmsChannel);

		CmsArticleCondition cmsArticleCondition = new CmsArticleCondition();
		PageBean<CmsArticle> pageBean = cmsArticleService.selectArticleByPaging(page,3,null,null,cmsArticleCondition);

		modelAndView.addObject("pageList",pageBean);
		modelAndView.addObject("cmsChannel",cmsChannel);
		modelAndView.addObject("member",member);
		modelAndView.setViewName(paramBiz.getWebsitTemplatesPrefix()+"blog/list");
		return modelAndView;
	}

	/**
	 * 内容页跳转
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value={"{username}/article/{id}"},method=RequestMethod.GET)
	public ModelAndView article(ModelAndView modelAndView, @PathVariable("username") String username, @PathVariable("id") Long id,HttpServletRequest request) throws IOException, ServletException {
		CmsChannel cmsChannel = new CmsChannel();
		cmsChannel.setChannelDir("blog");
		cmsChannel = cmsChannelService.selectOne(cmsChannel);
		try {
			Member member = memberService.getMemberByUsername(username);
			if(member == null){
				modelAndView.setViewName("redirect:/index");
				return modelAndView;
			}

			PageBean<MemberVisitorLog> pageBean =new PageBean<>(1,9,"createTime","asc");
			MemberVisitorLogCondition memberVisitorLogCondition = new MemberVisitorLogCondition();
			memberVisitorLogCondition.setMemberId(member.getId());
			pageBean= memberVisitorLogService.selectMemberVisitorLogByPaging(pageBean,memberVisitorLogCondition);

			CmsArticle	cmsArticle = cmsArticleService.getArticleById(id);
			CmsArticle	cmsAfterArticle = cmsArticleService.getAfterArticle(id,cmsChannel.getId());
			CmsArticle	cmsPrevArticle = cmsArticleService.getPrevArticle(id,cmsChannel.getId());

			modelAndView.addObject("cmsArticle",cmsArticle);
			modelAndView.addObject("cmsAfterArticle",cmsAfterArticle);
			modelAndView.addObject("cmsPrevArticle",cmsPrevArticle);

			modelAndView.addObject("cmsChannel",cmsChannel);
			modelAndView.addObject("member",member);
			modelAndView.addObject("memberVisitorLog",pageBean);

			modelAndView.setViewName(paramBiz.getWebsitTemplatesPrefix()+"blog/article");

		} catch (Exception e) {
			String message = messageSource.getMessage("server.error.500",new Object[]{}, request.getLocale());
			logger.error(message,e);
		}

		return modelAndView;
	}

}
